package GUI;

import Interfaces.*;
import Network.*;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/**
 * Provides the GUI and its functionality
 * 
 * @author Paul Thorwirth
 * @version 1.4
 *
 * @see JFrame
 */
@SuppressWarnings("serial")
public class GUI extends JFrame implements Globals
{
	private NetworkLOG networkLOG;
	boolean started = false;

	private WindowAdapter windowAdapter;

	private JPanel leftPanel;
	private JPanel rightPanel;

	private JMenu preferencesMenu;
	private JMenuItem ipItem;
	private JMenuItem intervalItem;
	private JMenuItem dirItem;

	private JMenu aboutMenu;

	private JMenuBar menuBar;

	private JButton startButton;
	private JButton stopButton;

	private JLabel ipLabel;
	private JLabel dirLabel;
	private JLabel checkIntervalLabel;

	private JTextField ipBox;
	private JTextField dirBox;
	private JTextField checkIntervalBox;

	/**
	 * Initializes the GUI
	 * <p>
	 * Things that are done here:
	 * <p>
	 * <ol>
	 *		<li>Define and add WindowAdapter</li>
	 *		<li>Set size and title of main window</li>
	 *		<li>Set root layout to GridLayout</li>
	 *		<li>Add Left Panel with Buttons and ActionListeners</li>
	 *		<li>Add Right Panel with Labels and TextFields including ActionListeners</li>
	 *		<li>Add Preferences menu with Items and ActionListeners</li>
	 *		<li>Add About menu with MouseAdapter</li>
	 *		<li>Add menus to main window</li>
	 * </ol>
	 * 
	 * @see WindowAdapter
	 * @see JPanel
	 * @see JButton
	 * @see JLabel
	 * @see JTextField
	 * @see JMenu
	 * @see JMenuItem
	 * @see ActionListener
	 */
	public GUI()
	{
		//add WindowAdapter to listen on window events
		windowAdapter = new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e) 
			{
				super.windowClosing(e);
				stop();
				System.exit(0);
			}
		};
		this.addWindowListener(windowAdapter);
		
		//set size and title
		this.setSize(320, 240);
		this.setTitle("Ping LOG");

		//set root Layout (x row, 2 columns, 2px gaps)
		this.setLayout(new GridLayout(0, 2, 2, 2));

		//left Panel with buttons
		leftPanel = new JPanel();
		leftPanel.setSize(new Dimension(this.getWidth()/2, this.getHeight()));
		leftPanel.setLayout(new GridLayout(0, 1, 2, 2)); //left panel with 1 columns, x rows, 2px gaps

		// add leftPanel to main window
		this.add(leftPanel);

		//StartButton
		startButton = new JButton("Start");
		startButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				start();
			}	
		});
		leftPanel.add(startButton);

		//StopButton
		stopButton = new JButton("Stop");
		stopButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				stop();
			}	
		});
		leftPanel.add(stopButton);

		//right Panel with labels and text fields
		rightPanel = new JPanel();
		rightPanel.setSize(new Dimension(this.getWidth()/2, this.getHeight()));
		rightPanel.setLayout(new GridLayout(0, 1, 2, 2)); //x rows, 1 column, 2px gaps
		rightPanel.setBackground(Color.LIGHT_GRAY);

		//add the rightPanel to main window
		this.add(rightPanel);

		//the Labels and TextFields
		ipLabel = new JLabel("IP :");
		dirLabel = new JLabel("Output Directory :");
		checkIntervalLabel = new JLabel("Checking Interval :");

		ipBox = new JTextField("easy.box");
		dirBox = new JTextField("out");
		checkIntervalBox = new JTextField("60");

		//ipBox ActionListener
		ipBox.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				stop();
			}
		});
		
		//dirBox ActionListener
		dirBox.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				stop();
			}
		});

		//checkIntervalBox ActionListener
		checkIntervalBox.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				stop();
			}
		});

		//add all items to right Panel
		rightPanel.add(ipLabel);
		rightPanel.add(ipBox);
		rightPanel.add(dirLabel);
		rightPanel.add(dirBox);
		rightPanel.add(checkIntervalLabel);
		rightPanel.add(checkIntervalBox);

		//preferences menu
		preferencesMenu = new JMenu("Preferences");

		//items for the menu
		ipItem = new JMenuItem("IP");
		intervalItem = new JMenuItem("Check Interval");
		dirItem = new JMenuItem("Output Directory");

		//ipItem ActionListener
		ipItem.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				String temp = JOptionPane.showInputDialog("Enter IP - Adress to check");
				ipBox.setText(temp);
				stop();
			}
		});

		//dirItem ActionListener
		dirItem.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				String temp = JOptionPane.showInputDialog("Enter IP - Adress to check");
				dirBox.setText(temp);
				stop();
			}
		});

		//intervalIten ActionListener
		intervalItem.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				String temp = JOptionPane.showInputDialog("Enter IP - Adress to check");
				checkIntervalBox.setText(temp);
				stop();
			}
		});

		//add Items to menu
		preferencesMenu.add(ipItem);
		preferencesMenu.add(dirItem);
		preferencesMenu.add(intervalItem);

		//about menu
		aboutMenu = new JMenu("About");
		aboutMenu.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				JOptionPane.showMessageDialog(null, "Created by Paul Thorwirth");
			}
		});
		
		//the menu bar
		menuBar = new JMenuBar();
		menuBar.add(preferencesMenu);
		menuBar.add(Box.createHorizontalGlue()); //for about menu to be on right side
		menuBar.add(aboutMenu);
		this.setJMenuBar(menuBar);
	}

	/**
	 * Described in Globals Interface. 
	 * Gets the Text of the TextFields and passes them to the constructor of NetworkLOG-class.
	 * After that NetworkLOG.start() is called to start the logging.
	 * 
	 * @see Globals
	 * @see NetworkLOG#start()
	 */
	public void start() 
	{
		String ip = ipBox.getText();
		String dir = dirBox.getText();
		int checkInterval = 0;
		boolean intOK = true;
		
		try
		{
			checkInterval = Integer.parseInt(checkIntervalBox.getText());
		} catch (NumberFormatException e) { intOK = false; }
		if (intOK) 
		{
			networkLOG = new NetworkLOG(ip, dir, checkInterval);
			networkLOG.start();
			this.setTitle("PingLOG - running");
			started = true;
		} else JOptionPane.showMessageDialog(null, "No valid Integer has been entered in Check Interval Box! Try again :P");
	}

	/**
	 * Described in Globals Interface
	 * 
	 * @see Globals
	 * @see NetworkLOG#stop()
	 */
	public void stop() 
	{
		if (started) networkLOG.stop();
		this.setTitle("PingLOG - stopped");
	}
}